package com.lap.auth.domain.module.service;

import com.lap.auth.domain.module.Module;
import com.lap.auth.domain.module.repository.ModuleRepository;
import com.lap.auth.shared.errors.UserError;
import com.lap.framework.common.exception.BizException;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ModuleValidator {

  private final ModuleRepository repository;

  public void validateRouteIdUnique(String routeId) {
    repository
        .findByRouteId(routeId)
        .ifPresent(module -> BizException.interrupt(UserError.MODULE_EXISTS));
  }

  public Module assertModuleExists(Integer id) {
    return repository
        .findById(id)
        .orElseThrow(() -> BizException.newBiz(UserError.MODULE_NOT_FOUND));
  }
}
